const app = getApp();
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    index: null,
    index2: null,
    index3: null,
    region: ['安徽省', '合肥市', '蜀山区'],
    picker: ['全民经纪人', '中介机构经纪人'],
    isInter:false,
    imgList: [],
    formdata:'',
    modalName: null,
    textareaAValue: '',
    textareaBValue: '',
    isName:"",
    isTel:"",
    isCom:"",
    isWechat:"",
    uploadImgs:null,
    citypicker:[],
    cityCodeList:[]
  },
	onLoad(){  
	},
  onShow() {
    let open_id = wx.getStorageSync('user_new')
    console.log(open_id)
    app.core('get', 'findAgentByOpenid?open_id=' + open_id, '', (res) => {
      console.log(res)
      let resultCode = res.resultCode
      if (resultCode === 500) {
        return true
      }
      if (resultCode === 200){
        let status = res.data.status
        if (status === 0) {
          wx.showModal({
            title: '提示',
            content: '您已注册，后台正在审核中，请勿重复注册',
            success(res) {
              if (res.confirm) {
                // console.log('用户点击确定')
                setTimeout(function () {
                  wx.reLaunch({
                    url: '/pages/member/index',
                  });
                }, 500)
              } else if (res.cancel) {
                // console.log('用户点击取消')
                setTimeout(function () {
                  wx.reLaunch({
                    url: '/pages/member/index',
                  });
                }, 500)
              }
            }
          })
        }
        if (status === 2) {
          wx.showModal({
            title: '提示',
            content: '您已注册成功，请勿重复注册',
            success(res) {
              if (res.confirm) {
                // console.log('用户点击确定')
                setTimeout(function () {
                  wx.reLaunch({
                    url: '/pages/member/index',
                  });
                }, 500)
              } else if (res.cancel) {
                // console.log('用户点击取消')
                setTimeout(function () {
                  wx.reLaunch({
                    url: '/pages/member/index',
                  });
                }, 500)
              }
            }
          })
        }
      }
    })
    app.core('get','get_all_sites',{},(res) => {
      let citypicker = res.data
      let cityList = []
      let codeList = []
      for (let city of citypicker) {
        cityList.push(city.area)
        codeList.push(city.id)
      }
      console.log(cityList, codeList)
      this.setData({
        citypicker: cityList,
        cityCodeList: codeList
      })
    })
  },
  RegionChange: function (e) {
    this.setData({
      region: e.detail.value
    })
  },
  PickerChange(e) {
    console.log(e);
    this.setData({
      index2: e.detail.value
    })
    let index2 = this.data.index2;
    console.log(this.data.picker[index2])
    if(index2 === "1"){
      this.setData({
        isInter:true
      })
    }else{
      this.setData({
        isInter:false
      })
    }
  },
  cityChange(e) {
    this.setData({
      index3: e.detail.value
    })
    let index3 = this.data.index3;
    console.log(this.data.cityCodeList[index3])
  },
  nameVal(e){
    this.setData({
      isName: e.detail.value
    })
  },

  telVal(e) {
    this.setData({
      isTel: e.detail.value
    })
  },

  comVal(e) {
    this.setData({
      isCom: e.detail.value
    })
    // console.log(this.data.isCom)
  },

  wechatVal(e) {
    this.setData({
      isWechat: e.detail.value
    })
  },

  textareaAInput(e) {
    this.setData({
      textareaAValue: e.detail.value
    })
  },
  textareaBInput(e) {
    this.setData({
      textareaBValue: e.detail.value
    })
  },
  RegisterAgent() {
    var vm = this;
    var isTel = vm.data.isTel;
    var isName = vm.data.isName;
    let type = vm.data.index2
    if (vm.data.isName == '' || isTel == '' || vm.data.isWechat == '') {
      wx.showToast({
        title: '带 * 为必填',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    else if (!(/^[\u4E00-\u9FA5]{2,4}$/.test(isName))) {
      wx.showToast({
        title: '姓名格式错误！请重新输入',
        icon: 'none',
        duration: 2000
      });
      return false;
    } else if (!(/^1(3|4|5|6|7|8|9)\d{9}$/.test(isTel))) {
      wx.showToast({
        title: '手机号码格式错误！请重新输入',
        icon: 'none',
        duration: 2000
      });
      return false;
    } else if ( type == null ) {
      wx.showToast({
        title: '请选择经纪人类型',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    else {
      let name = vm.data.isName
      let phone = vm.data.isTel
      let wechatId = vm.data.isWechat
      let type = vm.data.index2
      let organ = vm.data.isCom
      let index3 = vm.data.index3
      let siteId = vm.data.cityCodeList[index3]
      let openid = wx.getStorageSync('user_new')      
      console.log(name, phone, siteId, wechatId, type, organ, openid)
      app.core('post', 'registerAgent', {
        'name': name,
        'phone': phone,
        'type': type,
        'wechatId': wechatId,
        'siteId': siteId,
        'organ': organ,
        'open_id': openid,
      }, (res) => {
        console.log(res);
        if (res.resultCode == 200) {
          wx.showToast({
            title: '注册成功，等待后台审核通过',
            icon: 'none',
            duration: 2000
          });
          setTimeout(function () {
            wx.switchTab({
              url: '/pages/member/index',
            });
          }, 1000)
        } else {
          let msg = res.msg
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          });
        }
      },
        (err) => {
          console.log(err)
        }
      )

    }

  }
})